'use strict';

/* eslint-disable */
$(function(){
  // var indicators = $('#indicators');
  // for (let i = 0, len = 3; i < len; i++ ) {
  //   var init = $('<li data-target="#carousel-example-generic"></li>');
  //   if (i === 0) {
  //     init.attr('data-slide-to', i).addClass('active');
  //   } else {
  //     init.attr("data-slide-to", i);
  //   }
  //   // init.attr("data-slide-to", i).addClass("active");  
  //   indicators.append(init);
  // }
  function monthFilter(time) {
    if (!time) {
      return "";
    }
    const month = [
      "Jan.",
      "Feb.",
      "Mar.",
      "Apr.",
      "May",
      "June",
      "July",
      "Aug.",
      "Sept.",
      "Oct.",
      "Nov.",
      "Dec.",
    ];
    const monthStr = time.split("-")[1];
    const num = Number(monthStr);
    return month[num - 1];
  };
  function dayFilter(time) {
    if (!time) {
      return "";
    }
    return time.split("-")[2];
  };
  function timeEnFilter(time){
    if (!time) {
      return "";
    }
    const month = [
      "Jan.",
      "Feb.",
      "Mar.",
      "Apr.",
      "May",
      "June",
      "July",
      "Aug.",
      "Sept.",
      "Oct.",
      "Nov.",
      "Dec.",
    ];
    const timeStr = time.split("-");
    const yearStr = timeStr[0];
    const monthStr = timeStr[1];
    const dayStr = timeStr[2];
    const num = Number(monthStr);
    return `${dayStr} ${month[num - 1]} ${yearStr}`;
  };


  $.get('http://39.106.171.39:8011/mbl/Index/getIndex', function(data, status){
    console.log('status:', status);
    console.log('data:', data);
    var pics = data.indexPics;
    var news = data.indexNews;
    var events = data.indexEvents;
    var researchs = data.indexResearchs;
    var indexMottos = data.indexMottos;
    // 轮播
    var indicators = $("#indicators");
    var listbox = $("#listbox");
    for (var i = 0; i < pics.length; i++) {
      var liItem = $('<li data-target="#carousel-example-generic"></li>');
      var item = $('<div class="item"></div>');
      if (i === 0) {
        liItem.attr('data-slide-to', i).addClass('active');
        item.addClass('active');
      } else {
        liItem.attr("data-slide-to", i);
      }
      // 生成li元素
      indicators.append(liItem);

      var img = $('<img />');
      img.attr("src", pics[i].picture || "../public/img/slide_demo.png");
      // img.attr("src", "../public/img/slide_demo.png");
      var captionWrapper = $('<div class="carousel-caption"></div>');
      var caption = $('<div class="wrapper"></div>');
      var captionTitle = $("<h3>");
      captionTitle.text(pics[i].title);
      var captionTxt = $("<p>");
      captionTxt.text(pics[i].subtitle);
      caption.append(captionTitle).append(captionTxt);
      captionWrapper.append(caption);
      // 生成items
      item.append(img).append(captionWrapper);
      listbox.append(item);
    }
    // 活动列表
    for (var i = 0; i < news.length && i < 3; i++) {
      // news
      var newsWrapper = $("#newsWrapper");
      var newItem = $('<div class="new-item"></div>');
      var img = $('<img>').attr("src", news[i].picture || "../public/img/image_demo.png");
      var p = $("<p></p>").text(news[i].newstitle);
      var span = $("<span></span>").text(timeEnFilter(news[i].newsdate));
      newItem.append(img).append(p).append(span);
      newsWrapper.append(newItem);
    }
    for (var i = 0; i < events.length && i < 3; i++) {
      // events
      var eventsWrapper = $("#eventsWrapper");
      var eventItem = $('<div class="event-item"></div>');
      
      var eventTime = $('<span class="event-time"></span>');
      var eventTimeDay = $('<span class="day"></span>');
      eventTimeDay.text(dayFilter(events[i].eventdate));
      var eventTimeMonth = $('<span class="month"></span>');
      eventTimeMonth.text(monthFilter(events[i].eventdate));
      eventTime.append(eventTimeDay).append(eventTimeMonth);
      eventItem.append(eventTime);

      var eventDetail = $('<div class="event-detail"></div>');
      var h3 = $("<h3></h3>").text(events[i].eventstitle);
      var p = $("<p></p>").text(events[i].address);
      var span = $("<span></span>").text(timeEnFilter(events[i].eventdate));
      eventDetail.append(h3).append(p).append(span);
      eventItem.append(eventDetail);

      eventsWrapper.append(eventItem);
    }
    // 研究方向
    var researchLeftImg = $("#research-content__left-img");
    researchLeftImg.attr("src", researchs[0].picture);
    var researchLeftTxt = $("#research-content__left-txt");
    researchLeftTxt.text(researchs[0].introduction);
    var researchRightImg = $("#research-content__right-img");
    researchRightImg.attr("src", researchs[1].picture);
    var researchRightTxt = $("#research-content__right-txt");
    researchRightTxt.text(researchs[1].introduction);

    // 格言首页
    var professorImg = $("#professor-img");
    professorImg.attr("src", indexMottos[0].picture);
    var professorName = $("#professor-name");
    professorName.text(indexMottos[0].teachername);
    var words = $("#words-desc");
    words.text(indexMottos[0].motto);
  });

  // setTimeout(function() {
  //   var test = [
  //     {
  //       "id": "3904264395d54fa8b48116c3856ba89f",
  //       "picture": "http://39.106.171.39:8080/pictures/eb62c45ff659466fb04b4a08ee04e15d1587781582513.jpg",
  //       "title": "xxx",
  //       "subtitle": "xxxx",
  //       "flag": "0"
  //     },
  //     {
  //       "id": "5c09cec7ac944e4aa9639a1cbb0daca6",
  //       "picture": "http://39.106.171.39:8080/pictures/eb62c45ff659466fb04b4a08ee04e15d1587781582513.jpg",
  //       "title": "gg",
  //       "subtitle": "aa",
  //       "flag": "0"
  //     }
  //   ];
  //   var indicators = $("#indicators");
  //   console.log("indicators", indicators);
  //   for (var i = 0; i < test; i++) {
  //     var init = $('<li data-target="#carousel-example-generic"></li>');
  //     if (i === 0) {
  //       init.attr("data-slide-to", i).addClass("active");
  //     } else {
  //       init.attr("data-slide-to", i);
  //     }
  //     // init.attr("data-slide-to", i).addClass("active");
  //     indicators.append(init);
  //   }
  // }, 500);
});
